
///
/// 张诚宇
///
/// 视频列表页面
///
/// 2022.6.14
///
///
///
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:zhangchengyu20220614/model/VideoListEntity.dart';
import 'package:zhangchengyu20220614/model/httputils.dart';
import 'package:zhangchengyu20220614/view/videomessage.dart';

class VideoPage extends StatefulWidget {
  const VideoPage({Key key}) : super(key: key);

  @override
  _VideoPageState createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> with AutomaticKeepAliveClientMixin{

  List<Result> _list=[];


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    NetWorkManaget().netWorkGet('http://mobile.bwstudent.com/hotNews/video/v1/findVideoList',map: {'columnId':1001,'page':1,'size':5},onSuccess: (data){
      setState(() {
         _list.clear();
         _list.addAll(VideoListEntity.fromJson(data).result);
      });
    });
  }



  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: _list.length<=0 ? Center(child: CircularProgressIndicator(color: Colors.deepOrange,),) : ListView.builder(itemBuilder: (context,index){
        return InkWell(
          onLongPress: (){
            Navigator.push(context, MaterialPageRoute(builder: (context)=>VideoMessagePage(result: _list[index],)));
          },
          child: Container(
            margin: EdgeInsets.only(top: 15),
            child: Stack(
              children: [
                getVideo(_list[index]),
                Positioned(child: Text(_list[index].title,style: TextStyle(color: Colors.white),),top: 0,)
              ],
            ),
          ),
        );
      },itemCount: _list.length,),
    );
  }

  getVideo(Result result) {
    VideoPlayerController videoPlayerController=VideoPlayerController.network(result.videoUrl);
    ChewieController chewieController=ChewieController(videoPlayerController: videoPlayerController,aspectRatio: 16/9,autoInitialize: true);
    return AspectRatio(aspectRatio: chewieController.aspectRatio,child: Chewie(controller: chewieController,),);
  }

  //卡顿处理
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
